﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002FF RID: 767
	internal class SkipObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x0600102D RID: 4141 RVA: 0x0004B010 File Offset: 0x00049410
		public SkipObservable(IObservable<T> source, int count) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.count = count;
		}

		// Token: 0x0600102E RID: 4142 RVA: 0x0004B02C File Offset: 0x0004942C
		public SkipObservable(IObservable<T> source, TimeSpan duration, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread || source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.duration = duration;
			this.scheduler = scheduler;
		}

		// Token: 0x0600102F RID: 4143 RVA: 0x0004B05D File Offset: 0x0004945D
		public IObservable<T> Combine(int count)
		{
			return new SkipObservable<T>(this.source, this.count + count);
		}

		// Token: 0x06001030 RID: 4144 RVA: 0x0004B072 File Offset: 0x00049472
		public IObservable<T> Combine(TimeSpan duration)
		{
			return (!(duration <= this.duration)) ? new SkipObservable<T>(this.source, duration, this.scheduler) : this;
		}

		// Token: 0x06001031 RID: 4145 RVA: 0x0004B09D File Offset: 0x0004949D
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			if (this.scheduler == null)
			{
				return this.source.Subscribe(new SkipObservable<T>.Skip(this, observer, cancel));
			}
			return new SkipObservable<T>.Skip_(this, observer, cancel).Run();
		}

		// Token: 0x0400097F RID: 2431
		private readonly IObservable<T> source;

		// Token: 0x04000980 RID: 2432
		private readonly int count;

		// Token: 0x04000981 RID: 2433
		private readonly TimeSpan duration;

		// Token: 0x04000982 RID: 2434
		internal readonly IScheduler scheduler;

		// Token: 0x02000300 RID: 768
		private class Skip : OperatorObserverBase<T, T>
		{
			// Token: 0x06001032 RID: 4146 RVA: 0x0004B0CB File Offset: 0x000494CB
			public Skip(SkipObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.remaining = parent.count;
			}

			// Token: 0x06001033 RID: 4147 RVA: 0x0004B0E1 File Offset: 0x000494E1
			public override void OnNext(T value)
			{
				if (this.remaining <= 0)
				{
					this.observer.OnNext(value);
				}
				else
				{
					this.remaining--;
				}
			}

			// Token: 0x06001034 RID: 4148 RVA: 0x0004B110 File Offset: 0x00049510
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001035 RID: 4149 RVA: 0x0004B148 File Offset: 0x00049548
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000983 RID: 2435
			private int remaining;
		}

		// Token: 0x02000301 RID: 769
		private class Skip_ : OperatorObserverBase<T, T>
		{
			// Token: 0x06001036 RID: 4150 RVA: 0x0004B180 File Offset: 0x00049580
			public Skip_(SkipObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001037 RID: 4151 RVA: 0x0004B194 File Offset: 0x00049594
			public IDisposable Run()
			{
				IDisposable disposable = this.parent.scheduler.Schedule(this.parent.duration, new Action(this.Tick));
				IDisposable disposable2 = this.parent.source.Subscribe(this);
				return StableCompositeDisposable.Create(disposable, disposable2);
			}

			// Token: 0x06001038 RID: 4152 RVA: 0x0004B1E2 File Offset: 0x000495E2
			private void Tick()
			{
				this.open = true;
			}

			// Token: 0x06001039 RID: 4153 RVA: 0x0004B1ED File Offset: 0x000495ED
			public override void OnNext(T value)
			{
				if (this.open)
				{
					this.observer.OnNext(value);
				}
			}

			// Token: 0x0600103A RID: 4154 RVA: 0x0004B20C File Offset: 0x0004960C
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600103B RID: 4155 RVA: 0x0004B244 File Offset: 0x00049644
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000984 RID: 2436
			private readonly SkipObservable<T> parent;

			// Token: 0x04000985 RID: 2437
			private volatile bool open;
		}
	}
}
